Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ncc on Acorn to circumvent npm issue #6460

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lerna": "lerna",
"dev": "lerna run build --stream --parallel",
"testonly": "jest",
"testall": "yarn check && yarn run testonly -- --coverage --forceExit --runInBand --reporters=default --reporters=jest-junit",
"testall": "yarn check && rm -rf node_modules/acorn && yarn run testonly -- --coverage --forceExit --runInBand --reporters=default --reporters=jest-junit",
"pretest": "yarn run lint",
"test": "yarn run testall || yarn run testall",
"coveralls": "cat ./test/coverage/lcov.info | coveralls",
Expand Down Expand Up @@ -61,6 +61,7 @@
"@zeit/next-sass": "1.0.2-canary.2",
"@zeit/next-typescript": "1.1.2-canary.0",
"abort-controller": "2.0.2",
"acorn": "6.1.0",
"amphtml-validator": "1.0.23",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"devDependencies": {
"@taskr/clear": "1.1.0",
"@taskr/watch": "1.1.0",
"@types/react": "16.8.4",
"@types/react": "16.8.5",
"@types/react-dom": "16.8.2",
"@types/react-is": "16.5.0",
"@types/send": "0.14.4",
Expand Down
2 changes: 2 additions & 0 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('module-alias').addAlias('acorn', 'next/dist/compiled/acorn')

import { join } from 'path'
import nanoid from 'nanoid'
import loadConfig from 'next-server/next-config'
Expand Down
1 change: 1 addition & 0 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"launch-editor": "2.2.1",
"loader-utils": "1.1.0",
"mkdirp": "0.5.1",
"module-alias": "2.2.0",
"nanoid": "1.2.1",
"next-server": "8.0.4-canary.3",
"prop-types": "15.6.2",
Expand Down
72 changes: 56 additions & 16 deletions packages/next/taskfile-ncc.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,67 @@
'use strict'

const ncc = require('@zeit/ncc')
const basename = require('path').basename
const dirname = require('path').dirname
const join = require('path').join
const { writeFileSync, existsSync, copyFileSync } = require('fs')
const { basename, dirname, extname, join, relative } = require('path')

module.exports = function (task) {
task.plugin('ncc', {}, function * (file, options) {
return ncc(
join(__dirname, file.dir, file.base),
{
// cannot bundle
externals: ['chokidar'],
...options
}
).then(({ code, assets }) => {
Object.keys(assets).forEach((key) => this._.files.push({
dir: join(file.dir, dirname(key)),
base: basename(key),
data: assets[key].source
}))
return ncc(join(__dirname, file.dir, file.base), {
// cannot bundle
externals: ['chokidar'],
...options
}).then(({ code, assets }) => {
Object.keys(assets).forEach(key =>
this._.files.push({
dir: join(file.dir, dirname(key)),
base: basename(key),
data: assets[key].source
})
)

file.data = Buffer.from(code, 'utf8')
})
})
}

// This function writes a minimal `package.json` file for a compiled package.
// It defines `name` and `main`. It also defines `types` (intended for
// development usage only).
module.exports.writePackageManifest = function writePackageManifest (
packageName
) {
const packagePath = require.resolve(packageName + '/package.json')
let { name, main, author, license, types, typings } = require(packagePath)
if (!main) {
main = 'index.js'
}

let typesFile = types || typings
if (typesFile) {
typesFile = require.resolve(join(packageName, typesFile))
}

const compiledPackagePath = join(__dirname, `dist/compiled/${packageName}`)

const potentialLicensePath = join(dirname(packagePath), './LICENSE')
if (existsSync(potentialLicensePath)) {
copyFileSync(potentialLicensePath, join(compiledPackagePath, './LICENSE'))
}

writeFileSync(
join(compiledPackagePath, './package.json'),
JSON.stringify(
Object.assign(
{},
{ name, main: `${basename(main, '.' + extname(main))}` },
author ? { author } : undefined,
license ? { license } : undefined,
typesFile
? {
types: relative(compiledPackagePath, typesFile)
}
: undefined
)
) + '\n'
)
}
17 changes: 16 additions & 1 deletion packages/next/taskfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const notifier = require('node-notifier')
const relative = require('path').relative
const { writePackageManifest } = require('./taskfile-ncc')

const babelOpts = {
presets: [
['@babel/preset-env', {
Expand All @@ -18,6 +21,18 @@ const babelOpts = {
]
}

export async function nccAcorn (task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('acorn')))
.ncc()
.target('dist/compiled/acorn')
writePackageManifest('acorn')
}

export async function precompile (task) {
await task.parallel(['nccAcorn'])
}

export async function compile (task) {
await task.parallel(['cli', 'bin', 'server', 'nextbuild', 'nextbuildstatic', 'pages', 'lib', 'client'])
}
Expand Down Expand Up @@ -63,7 +78,7 @@ export async function pages (task, opts) {
}

export async function build (task) {
await task.serial(['compile'])
await task.serial(['precompile', 'compile'])
}

export default async function (task) {
Expand Down
4 changes: 4 additions & 0 deletions packages/next/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"compilerOptions": {
"baseUrl": "./",
"strict": true,
"allowJs": true,
"noEmit": true,
"module": "esnext",
"target": "ES2017",
"esModuleInterop": true,
"moduleResolution": "node",
"paths": {
"acorn": ["dist/compiled/acorn"]
},
"jsx": "react"
}
}