refactor: nuxt3 utils

This commit is contained in:
Pooya Parsa 2021-03-29 11:33:38 +02:00
parent 5b146c0c88
commit 823e1a1eb4
8 changed files with 32 additions and 31 deletions

View File

@ -1,24 +0,0 @@
// Add polyfill imports to the first file encountered.
module.exports = ({ _types }) => {
let entryFile
return {
name: 'inject-polyfills',
visitor: {
Program (path, state) {
if (!entryFile) {
entryFile = state.filename
} else if (state.filename !== entryFile) {
return
}
const { polyfills } = state.opts
const { createImport } = require('@babel/preset-env/lib/utils')
// Imports are injected in reverse order
polyfills.slice().reverse().forEach((p) => {
createImport(path, p)
})
}
}
}
}

View File

@ -2,7 +2,7 @@ import { join, relative } from 'path'
import fsExtra from 'fs-extra' import fsExtra from 'fs-extra'
import { debounce } from 'lodash' import { debounce } from 'lodash'
import { DeterminedGlobals, determineGlobals } from '@nuxt/kit' import { DeterminedGlobals, determineGlobals } from '@nuxt/kit'
import { Nuxt } from '../core' import { Nuxt } from './nuxt'
import { import {
templateData, templateData,
compileTemplates, compileTemplates,
@ -11,7 +11,7 @@ import {
} from './template' } from './template'
import { createWatcher } from './watch' import { createWatcher } from './watch'
import { createApp, NuxtApp } from './app' import { createApp, NuxtApp } from './app'
import Ignore from './ignore' import Ignore from './utils/ignore'
export class Builder { export class Builder {
nuxt: Nuxt nuxt: Nuxt

View File

@ -2,7 +2,7 @@ import { join, relative, dirname } from 'path'
import fsExtra from 'fs-extra' import fsExtra from 'fs-extra'
import globby from 'globby' import globby from 'globby'
import lodashTemplate from 'lodash/template' import lodashTemplate from 'lodash/template'
import * as nxt from './nxt' import * as nxt from './utils/nxt'
export interface NuxtTemplate { export interface NuxtTemplate {
src: string // Absolute path to source file src: string // Absolute path to source file

View File

@ -1,6 +1,6 @@
import { resolve } from 'path' import { resolve } from 'path'
import globby from 'globby' import globby from 'globby'
import { Builder } from './builder' import { Builder } from '../builder'
// TODO: move to core resolver // TODO: move to core resolver
export async function resolveFiles (builder: Builder, pattern: string, srcDir: string) { export async function resolveFiles (builder: Builder, pattern: string, srcDir: string) {

View File

@ -1,7 +1,7 @@
import { basename, extname } from 'path' import { basename, extname } from 'path'
import hash from 'hash-sum' import hash from 'hash-sum'
import { camelCase } from 'scule' import { camelCase } from 'scule'
import { NuxtRoute } from './pages' import { NuxtRoute } from '../pages'
// NXT is a set of utils for serializing JavaScript data to JS code // NXT is a set of utils for serializing JavaScript data to JS code
export const serialize = data => JSON.stringify(data, null, 2).replace(/"{(.+)}"/g, '$1') export const serialize = data => JSON.stringify(data, null, 2).replace(/"{(.+)}"/g, '$1')

View File

@ -1,7 +1,7 @@
import chokidar, { WatchOptions } from 'chokidar' import chokidar, { WatchOptions } from 'chokidar'
import defu from 'defu' import defu from 'defu'
import consola from 'consola' import consola from 'consola'
import Ignore from './ignore' import Ignore from './utils/ignore'
export function createWatcher ( export function createWatcher (
pattern: string, pattern: string,

View File

@ -122,7 +122,7 @@ module.exports = (api, options = {}) => {
} }
) )
) )
plugins.push([require('./polyfills-plugin'), { polyfills }]) plugins.push([polyfillsPlugin, { polyfills }])
} }
// Pass options along to babel-preset-env // Pass options along to babel-preset-env
@ -175,3 +175,28 @@ module.exports = (api, options = {}) => {
plugins plugins
} }
} }
// Add polyfill imports to the first file encountered.
function polyfillsPlugin ({ _types }) {
let entryFile
return {
name: 'inject-polyfills',
visitor: {
Program (path, state) {
if (!entryFile) {
entryFile = state.filename
} else if (state.filename !== entryFile) {
return
}
const { polyfills } = state.opts
const { createImport } = require('@babel/preset-env/lib/utils')
// Imports are injected in reverse order
polyfills.slice().reverse().forEach((p) => {
createImport(path, p)
})
}
}
}
}