Unverified Commit 005a8abe authored by Guy Bedford's avatar Guy Bedford Committed by GitHub

feat: Webpack loader inlining (#21127)

This picks up on the inlining work in https://github.com/vercel/next.js/pull/20598 to also include webpack loader inlining optimizations.

This includes:
* The dependencies of sass-loader
* resolve-url-loader

And for added benefit:
* babel-plugin-transform-define
* babel-plugin-transform-react-remove-prop-types

style-loader and css-loader didn't inline easily. Perhaps we can come back to these ones.
parent dc87a843
...@@ -198,7 +198,7 @@ module.exports = ( ...@@ -198,7 +198,7 @@ module.exports = (
], ],
require('./plugins/amp-attributes'), require('./plugins/amp-attributes'),
isProduction && [ isProduction && [
require('babel-plugin-transform-react-remove-prop-types'), require('next/dist/compiled/babel/plugin-transform-react-remove-prop-types'),
{ {
removeImport: true, removeImport: true,
}, },
......
import loaderUtils from 'loader-utils' import loaderUtils from 'next/dist/compiled/loader-utils'
import path from 'path' import path from 'path'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
......
// import babel from 'next/dist/compiled/babel/core' // import babel from 'next/dist/compiled/babel/core'
import loaderUtils from 'loader-utils' import loaderUtils from 'next/dist/compiled/loader-utils'
import { tracer, traceAsyncFn, traceFn } from '../../../../tracer' import { tracer, traceAsyncFn, traceFn } from '../../../../tracer'
import cache from './cache' import cache from './cache'
import transform from './transform' import transform from './transform'
......
import loaderUtils from 'loader-utils' import loaderUtils from 'next/dist/compiled/loader-utils'
module.exports = function (content, sourceMap) { module.exports = function (content, sourceMap) {
this.cacheable() this.cacheable()
......
import chalk from 'chalk' import chalk from 'chalk'
import loaderUtils from 'loader-utils' import loaderUtils from 'next/dist/compiled/loader-utils'
import path from 'path' import path from 'path'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
......
...@@ -155,7 +155,7 @@ const customBabelLoader = babelLoader((babel) => { ...@@ -155,7 +155,7 @@ const customBabelLoader = babelLoader((babel) => {
} }
options.plugins.push([ options.plugins.push([
require.resolve('babel-plugin-transform-define'), require.resolve('next/dist/compiled/babel/plugin-transform-define'),
{ {
'process.env.NODE_ENV': development ? 'development' : 'production', 'process.env.NODE_ENV': development ? 'development' : 'production',
'typeof window': isServer ? 'undefined' : 'object', 'typeof window': isServer ? 'undefined' : 'object',
......
import loaderUtils from 'loader-utils' import loaderUtils from 'next/dist/compiled/loader-utils'
import { tracer, traceFn } from '../../tracer' import { tracer, traceFn } from '../../tracer'
export type ClientPagesLoaderOptions = { export type ClientPagesLoaderOptions = {
......
import NativeModule from 'module' import NativeModule from 'module'
import loaderUtils from 'loader-utils' import loaderUtils from 'next/dist/compiled/loader-utils'
import { import {
webpack, webpack,
isWebpack5, isWebpack5,
......
...@@ -36,10 +36,18 @@ function pluginSyntaxJsx() { ...@@ -36,10 +36,18 @@ function pluginSyntaxJsx() {
return require('@babel/plugin-syntax-jsx') return require('@babel/plugin-syntax-jsx')
} }
function pluginTransformDefine() {
return require('babel-plugin-transform-define')
}
function pluginTransformModulesCommonjs() { function pluginTransformModulesCommonjs() {
return require('@babel/plugin-transform-modules-commonjs') return require('@babel/plugin-transform-modules-commonjs')
} }
function pluginTransformReactRemovePropTypes() {
return require('babel-plugin-transform-react-remove-prop-types')
}
function pluginTransformRuntime() { function pluginTransformRuntime() {
return require('@babel/plugin-transform-runtime') return require('@babel/plugin-transform-runtime')
} }
...@@ -66,7 +74,9 @@ module.exports = { ...@@ -66,7 +74,9 @@ module.exports = {
pluginSyntaxBigint, pluginSyntaxBigint,
pluginSyntaxDynamicImport, pluginSyntaxDynamicImport,
pluginSyntaxJsx, pluginSyntaxJsx,
pluginTransformDefine,
pluginTransformModulesCommonjs, pluginTransformModulesCommonjs,
pluginTransformReactRemovePropTypes,
pluginTransformRuntime, pluginTransformRuntime,
presetEnv, presetEnv,
presetReact, presetReact,
......
module.exports = require('./bundle').pluginTransformDefine()
module.exports = require('./bundle').pluginTransformReactRemovePropTypes()
This diff is collapsed.
module.exports = require('./bundle').pluginTransformDefine()
module.exports = require('./bundle').pluginTransformReactRemovePropTypes()
This diff is collapsed.
module.exports=(()=>{"use strict";var e={764:e=>{e.exports=JSON.parse('{"additionalProperties":true,"properties":{"name":{"description":"The filename template for the target file(s) (https://github.com/webpack-contrib/file-loader#name).","anyOf":[{"type":"string"},{"instanceof":"Function"}]},"outputPath":{"description":"A filesystem path where the target file(s) will be placed (https://github.com/webpack-contrib/file-loader#outputpath).","anyOf":[{"type":"string"},{"instanceof":"Function"}]},"publicPath":{"description":"A custom public path for the target file(s) (https://github.com/webpack-contrib/file-loader#publicpath).","anyOf":[{"type":"string"},{"instanceof":"Function"}]},"postTransformPublicPath":{"description":"A custom transformation function for post-processing the publicPath (https://github.com/webpack-contrib/file-loader#posttransformpublicpath).","instanceof":"Function"},"context":{"description":"A custom file context (https://github.com/webpack-contrib/file-loader#context).","type":"string"},"emitFile":{"description":"Enables/Disables emit files (https://github.com/webpack-contrib/file-loader#emitfile).","type":"boolean"},"regExp":{"description":"A Regular Expression to one or many parts of the target file path. The capture groups can be reused in the name property using [N] placeholder (https://github.com/webpack-contrib/file-loader#regexp).","anyOf":[{"type":"string"},{"instanceof":"RegExp"}]},"esModule":{"description":"By default, file-loader generates JS modules that use the ES modules syntax.","type":"boolean"}},"type":"object"}')},467:(e,t,i)=>{const r=i(206);e.exports=r.default;e.exports.raw=r.raw},206:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:true});t.default=loader;t.raw=void 0;var r=_interopRequireDefault(i(622));var o=_interopRequireDefault(i(710));var a=_interopRequireDefault(i(225));var n=_interopRequireDefault(i(764));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function loader(e){const t=o.default.getOptions(this);(0,a.default)(n.default,t,{name:"File Loader",baseDataPath:"options"});const i=t.context||this.rootContext;const s=o.default.interpolateName(this,t.name||"[contenthash].[ext]",{context:i,content:e,regExp:t.regExp});let p=s;if(t.outputPath){if(typeof t.outputPath==="function"){p=t.outputPath(s,this.resourcePath,i)}else{p=r.default.posix.join(t.outputPath,s)}}let u=`__webpack_public_path__ + ${JSON.stringify(p)}`;if(t.publicPath){if(typeof t.publicPath==="function"){u=t.publicPath(s,this.resourcePath,i)}else{u=`${t.publicPath.endsWith("/")?t.publicPath:`${t.publicPath}/`}${s}`}u=JSON.stringify(u)}if(t.postTransformPublicPath){u=t.postTransformPublicPath(u)}if(typeof t.emitFile==="undefined"||t.emitFile){this.emitFile(p,e)}const l=typeof t.esModule!=="undefined"?t.esModule:true;return`${l?"export default":"module.exports ="} ${u};`}const s=true;t.raw=s},710:e=>{e.exports=require("loader-utils")},225:e=>{e.exports=require("next/dist/compiled/schema-utils")},622:e=>{e.exports=require("path")}};var t={};function __nccwpck_require__(i){if(t[i]){return t[i].exports}var r=t[i]={exports:{}};var o=true;try{e[i](r,r.exports,__nccwpck_require__);o=false}finally{if(o)delete t[i]}return r.exports}__nccwpck_require__.ab=__dirname+"/";return __nccwpck_require__(467)})(); module.exports=(()=>{"use strict";var e={764:e=>{e.exports=JSON.parse('{"additionalProperties":true,"properties":{"name":{"description":"The filename template for the target file(s) (https://github.com/webpack-contrib/file-loader#name).","anyOf":[{"type":"string"},{"instanceof":"Function"}]},"outputPath":{"description":"A filesystem path where the target file(s) will be placed (https://github.com/webpack-contrib/file-loader#outputpath).","anyOf":[{"type":"string"},{"instanceof":"Function"}]},"publicPath":{"description":"A custom public path for the target file(s) (https://github.com/webpack-contrib/file-loader#publicpath).","anyOf":[{"type":"string"},{"instanceof":"Function"}]},"postTransformPublicPath":{"description":"A custom transformation function for post-processing the publicPath (https://github.com/webpack-contrib/file-loader#posttransformpublicpath).","instanceof":"Function"},"context":{"description":"A custom file context (https://github.com/webpack-contrib/file-loader#context).","type":"string"},"emitFile":{"description":"Enables/Disables emit files (https://github.com/webpack-contrib/file-loader#emitfile).","type":"boolean"},"regExp":{"description":"A Regular Expression to one or many parts of the target file path. The capture groups can be reused in the name property using [N] placeholder (https://github.com/webpack-contrib/file-loader#regexp).","anyOf":[{"type":"string"},{"instanceof":"RegExp"}]},"esModule":{"description":"By default, file-loader generates JS modules that use the ES modules syntax.","type":"boolean"}},"type":"object"}')},467:(e,t,i)=>{const r=i(206);e.exports=r.default;e.exports.raw=r.raw},206:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:true});t.default=loader;t.raw=void 0;var r=_interopRequireDefault(i(622));var o=_interopRequireDefault(i(443));var a=_interopRequireDefault(i(225));var n=_interopRequireDefault(i(764));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function loader(e){const t=o.default.getOptions(this);(0,a.default)(n.default,t,{name:"File Loader",baseDataPath:"options"});const i=t.context||this.rootContext;const s=o.default.interpolateName(this,t.name||"[contenthash].[ext]",{context:i,content:e,regExp:t.regExp});let p=s;if(t.outputPath){if(typeof t.outputPath==="function"){p=t.outputPath(s,this.resourcePath,i)}else{p=r.default.posix.join(t.outputPath,s)}}let u=`__webpack_public_path__ + ${JSON.stringify(p)}`;if(t.publicPath){if(typeof t.publicPath==="function"){u=t.publicPath(s,this.resourcePath,i)}else{u=`${t.publicPath.endsWith("/")?t.publicPath:`${t.publicPath}/`}${s}`}u=JSON.stringify(u)}if(t.postTransformPublicPath){u=t.postTransformPublicPath(u)}if(typeof t.emitFile==="undefined"||t.emitFile){this.emitFile(p,e)}const l=typeof t.esModule!=="undefined"?t.esModule:true;return`${l?"export default":"module.exports ="} ${u};`}const s=true;t.raw=s},443:e=>{e.exports=require("next/dist/compiled/loader-utils")},225:e=>{e.exports=require("next/dist/compiled/schema-utils")},622:e=>{e.exports=require("path")}};var t={};function __nccwpck_require__(i){if(t[i]){return t[i].exports}var r=t[i]={exports:{}};var o=true;try{e[i](r,r.exports,__nccwpck_require__);o=false}finally{if(o)delete t[i]}return r.exports}__nccwpck_require__.ab=__dirname+"/";return __nccwpck_require__(467)})();
\ No newline at end of file \ No newline at end of file
Copyright JS Foundation and other contributors
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This diff is collapsed.
{"name":"loader-utils","main":"index.js","author":"Tobias Koppers @sokra","license":"MIT"}
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -69,8 +69,6 @@ ...@@ -69,8 +69,6 @@
"@next/react-refresh-utils": "10.0.6-canary.1", "@next/react-refresh-utils": "10.0.6-canary.1",
"@opentelemetry/api": "0.14.0", "@opentelemetry/api": "0.14.0",
"ast-types": "0.13.2", "ast-types": "0.13.2",
"babel-plugin-transform-define": "2.0.0",
"babel-plugin-transform-react-remove-prop-types": "0.4.24",
"browserslist": "4.14.6", "browserslist": "4.14.6",
"buffer": "5.6.0", "buffer": "5.6.0",
"caniuse-lite": "^1.0.30001113", "caniuse-lite": "^1.0.30001113",
...@@ -82,8 +80,6 @@ ...@@ -82,8 +80,6 @@
"etag": "1.8.1", "etag": "1.8.1",
"find-cache-dir": "3.3.1", "find-cache-dir": "3.3.1",
"jest-worker": "24.9.0", "jest-worker": "24.9.0",
"klona": "^2.0.4",
"loader-utils": "2.0.0",
"native-url": "0.3.4", "native-url": "0.3.4",
"node-fetch": "2.6.1", "node-fetch": "2.6.1",
"node-html-parser": "1.4.9", "node-html-parser": "1.4.9",
...@@ -183,6 +179,8 @@ ...@@ -183,6 +179,8 @@
"async-retry": "1.2.3", "async-retry": "1.2.3",
"async-sema": "3.0.0", "async-sema": "3.0.0",
"babel-plugin-dynamic-import-node": "2.3.3", "babel-plugin-dynamic-import-node": "2.3.3",
"babel-plugin-transform-define": "2.0.0",
"babel-plugin-transform-react-remove-prop-types": "0.4.24",
"cacache": "15.0.5", "cacache": "15.0.5",
"cache-loader": "4.1.0", "cache-loader": "4.1.0",
"ci-info": "watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540", "ci-info": "watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540",
...@@ -205,6 +203,7 @@ ...@@ -205,6 +203,7 @@
"is-wsl": "2.2.0", "is-wsl": "2.2.0",
"json5": "2.1.3", "json5": "2.1.3",
"jsonwebtoken": "8.5.1", "jsonwebtoken": "8.5.1",
"loader-utils": "2.0.0",
"lodash.curry": "4.1.1", "lodash.curry": "4.1.1",
"lru-cache": "5.1.1", "lru-cache": "5.1.1",
"mkdirp": "0.5.3", "mkdirp": "0.5.3",
......
...@@ -39,22 +39,18 @@ const externals = { ...@@ -39,22 +39,18 @@ const externals = {
chalk: 'chalk', chalk: 'chalk',
'node-fetch': 'node-fetch', 'node-fetch': 'node-fetch',
// postcss: 'postcss',
// webpack // webpack
'node-libs-browser': 'node-libs-browser', 'node-libs-browser': 'node-libs-browser',
// css-loader
// postcss: 'postcss',
// sass-loader // sass-loader
// (also responsible for these dependencies in package.json) // (also responsible for these dependencies in package.json)
'node-sass': 'node-sass', 'node-sass': 'node-sass',
sass: 'sass', sass: 'sass',
fibers: 'fibers', fibers: 'fibers',
klona: 'klona',
chokidar: 'chokidar', chokidar: 'chokidar',
'loader-utils': 'loader-utils',
'jest-worker': 'jest-worker', 'jest-worker': 'jest-worker',
} }
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
...@@ -329,6 +325,14 @@ export async function ncc_jsonwebtoken(task, opts) { ...@@ -329,6 +325,14 @@ export async function ncc_jsonwebtoken(task, opts) {
.target('compiled/jsonwebtoken') .target('compiled/jsonwebtoken')
} }
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
externals['loader-utils'] = 'next/dist/compiled/loader-utils'
export async function ncc_loader_utils(task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('loader-utils')))
.ncc({ packageName: 'loader-utils', externals })
.target('compiled/loader-utils')
}
// eslint-disable-next-line camelcase
externals['lodash.curry'] = 'next/dist/compiled/lodash.curry' externals['lodash.curry'] = 'next/dist/compiled/lodash.curry'
export async function ncc_lodash_curry(task, opts) { export async function ncc_lodash_curry(task, opts) {
await task await task
...@@ -444,6 +448,7 @@ export async function ncc_sass_loader(task, opts) { ...@@ -444,6 +448,7 @@ export async function ncc_sass_loader(task, opts) {
...externals, ...externals,
'schema-utils': 'next/dist/compiled/schema-utils3', 'schema-utils': 'next/dist/compiled/schema-utils3',
}, },
target: 'es5',
}) })
.target('compiled/sass-loader') .target('compiled/sass-loader')
} }
...@@ -669,6 +674,7 @@ export async function ncc(task) { ...@@ -669,6 +674,7 @@ export async function ncc(task) {
'ncc_is_wsl', 'ncc_is_wsl',
'ncc_json5', 'ncc_json5',
'ncc_jsonwebtoken', 'ncc_jsonwebtoken',
'ncc_loader_utils',
'ncc_lodash_curry', 'ncc_lodash_curry',
'ncc_lru_cache', 'ncc_lru_cache',
'ncc_mkdirp', 'ncc_mkdirp',
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
/// <reference types="node" /> /// <reference types="node" />
declare module 'mini-css-extract-plugin' declare module 'mini-css-extract-plugin'
declare module 'loader-utils' declare module 'next/dist/compiled/loader-utils'
declare module 'next/dist/compiled/webpack/webpack' { declare module 'next/dist/compiled/webpack/webpack' {
import webpackSources from 'webpack-sources' import webpackSources from 'webpack-sources'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment