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 { debounce } from 'lodash'
import { DeterminedGlobals, determineGlobals } from '@nuxt/kit'
import { Nuxt } from '../core'
import { Nuxt } from './nuxt'
import {
templateData,
compileTemplates,
@ -11,7 +11,7 @@ import {
} from './template'
import { createWatcher } from './watch'
import { createApp, NuxtApp } from './app'
import Ignore from './ignore'
import Ignore from './utils/ignore'
export class Builder {
nuxt: Nuxt

View File

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

View File

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

View File

@ -1,7 +1,7 @@
import { basename, extname } from 'path'
import hash from 'hash-sum'
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
export const serialize = data => JSON.stringify(data, null, 2).replace(/"{(.+)}"/g, '$1')

View File

@ -1,7 +1,7 @@
import chokidar, { WatchOptions } from 'chokidar'
import defu from 'defu'
import consola from 'consola'
import Ignore from './ignore'
import Ignore from './utils/ignore'
export function createWatcher (
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
@ -175,3 +175,28 @@ module.exports = (api, options = {}) => {
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)
})
}
}
}
}